/**   
 * @Title: ShengFuCaiMainThread.java 
 * @Package com.esun.rabbit2.viewer.mainact.lotteryhallact 
 * @Description: 足球胜负彩购彩大厅刷新线程
 * @author: Billy Qigs@500wan.com   
 * @date: 2012-4-25 上午10:26:31  
 * Copyright: (C) 2011 Shenzhen E-sun Sky Net Technology Co.,ltd.
 */
package com.esun.rabbit2.viewer.mainact.lotteryhallact;

import java.util.ArrayList;
import java.util.Hashtable;

import android.content.Context;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.esun.config.Config;
import com.esun.config.Key;
import com.esun.config.Language;
import com.esun.rabbit2.communicaters.sportteryshengfu.ShengFuCaiCurrentExpectInfo;
import com.esun.rabbit2.viewer.mainact.lotteryhallact.shiyixuanwu.TempData;
import com.esun.ui.R;

/**
 * @ClassName: ShengFuCaiMainThread
 * @Description: 足球胜负彩购彩大厅刷新线程
 * @author: Billy Qigs@500wan.com
 * @date: 2012-4-25 上午10:26:31
 * 
 */
public class ShengFuCaiMainThread {

	private LinearLayout linearItem;
	private ProgressBar pb;
	private TextView tvMsg;
	// 显示期号和截止时间的布局
	private RelativeLayout relativeBottom;
	// 显示期号的view
	private TextView tvEpxect;
	// 显示截止时间的view
	private TextView tvTime;
	// 显示时钟图片的Image
	private ImageView imgTime;

	private Handler handler;
	private ShengFuCaiCurrentExpectInfo expectCommunicater;
	private TimeTickerCountDown timeTicker;
	private Context context;
	private String expect;
	// 截止时间
	private String endTime;
	// 剩余时间秒数
	private String leftTime;

	// 获取期号信息成功
	private final int GET_SHENGFUCAI_INFO_SUCCEED = 1001;
	// 获取期号信息失败
	private final int GET_SHENGFUCAI_INFO_FAILED = 1002;

	public ShengFuCaiMainThread(LinearLayout linear, Context con) {

		linearItem = linear;
		context = con;

		pb = (ProgressBar) this.linearItem.findViewById(R.id.lotteryhall_act_pb_shengfucai);
		tvMsg = (TextView) linearItem.findViewById(R.id.lotteryhall_act_tv_shengfucai_msg);
		relativeBottom = (RelativeLayout) linearItem.findViewById(R.id.lotteryhallact_relative_toshengfucai_team);
		tvEpxect = (TextView) linearItem.findViewById(R.id.lotteryhallact_tv_shengFuCaiExpect);
		tvTime = (TextView) linearItem.findViewById(R.id.lotteryhall_act_tv_shengFuCaiTime);
		imgTime = (ImageView) linearItem.findViewById(R.id.lotteryhallact_img_shengFuCaiTime);

		initHandler();

	}

	private void initHandler() {
		handler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				switch (msg.what) {
				// 获取期号信息成功
				case GET_SHENGFUCAI_INFO_SUCCEED:

					ArrayList<Hashtable<String, String>> resultList = expectCommunicater.getListResult();
					expect = resultList.get(0).get(Key.KEY_EXPECT);
					leftTime = resultList.get(0).get(Key.KEY_LEFTTIME);
					endTime = resultList.get(0).get(Key.KEY_BUYENDTIME);

					int index_one = endTime.indexOf("-");
					int index_tow = endTime.lastIndexOf(":");

					endTime = endTime.substring(index_one + 1, index_tow);

					// 判断是否在24小时之内，是则倒计时
					String showType = expectCommunicater.getListResult().get(0).get(Key.KEY_SHOWTYPE);
					if (showType.equals(Config.NUM0)) {
						// 显示倒计时
						restartTicker(leftTime);
						imgTime.setVisibility(View.VISIBLE);
					} else {
						// 显示截至时间
						tvTime.setText(endTime);
						imgTime.setVisibility(View.GONE);
					}
					tvEpxect.setText("第" + expect + "期");

					showGetInfoSucceedView();

					break;

				// 获取期号信息失败
				case GET_SHENGFUCAI_INFO_FAILED:

					showGetInfoFailedView();

					break;
				// 获取期号信息异常
				case Config.CONNECTION_FAIL:

					showGetInfoFailedView();

					break;
				}
				// 隐藏进度圈
				hideProgressBar();
			}
		};

	}

	// 获取胜负彩期号截止时间等信息
	public void refresh() {

		displayProgressBar();
		getShengFuInfoThread();
	}

	// 显示progressbar
	private void displayProgressBar() {
		pb.setVisibility(View.VISIBLE);
	}

	// 隐藏progressbar
	private void hideProgressBar() {
		pb.setVisibility(View.GONE);
	}

	// 获取胜负彩期号信息线程
	private void getShengFuInfoThread() {
		new Thread() {

			@Override
			public void run() {
				if (expectCommunicater == null) {
					expectCommunicater = new ShengFuCaiCurrentExpectInfo();
				}
				try {
					boolean isSucceed = expectCommunicater.isSucceedGetShengFuCaiExpectInfo();
					Message msg;
					if (isSucceed) {
						msg = handler.obtainMessage(GET_SHENGFUCAI_INFO_SUCCEED, null);
					} else {
						msg = handler.obtainMessage(GET_SHENGFUCAI_INFO_FAILED, null);
					}
					handler.sendMessage(msg);

				} catch (Exception e) {
					Message msg = handler.obtainMessage(Config.CONNECTION_FAIL, null);
					handler.sendMessage(msg);
				}

			}

		}.start();
	}

	/**
	 * @Title: showStopLotteryView
	 * @Description: 显示停止购彩布局
	 * @param: 设定文件
	 * @return: void 返回类型
	 * @throws
	 */
	public void showStopLotteryView() {

		relativeBottom.setVisibility(View.GONE);
		tvMsg.setText(Language.MSG_STOP_LOTTERY);

	}

	/**
	 * @Title: showGetInfoSucceedView
	 * @Description: 显示获取期号成功布局
	 * @param: 设定文件
	 * @return: void 返回类型
	 * @throws
	 */
	public void showGetInfoSucceedView() {

		relativeBottom.setVisibility(View.VISIBLE);
		tvMsg.setText(Language.MSG_SHENGFUCAI_LOTTERYHALL_TIP);

	}

	/**
	 * @Title: showGetInfoFailedView
	 * @Description: 显示获取期号失败布局
	 * @param: 设定文件
	 * @return: void 返回类型
	 * @throws
	 */
	public void showGetInfoFailedView() {

		relativeBottom.setVisibility(View.GONE);
		tvMsg.setText(Language.MSG_SHENGFUCAI_LOTTERYHALL_TIP);

	}

	/**
	 * @ClassName: TimeTickerCountDown
	 * @Description: 时钟
	 * @author: Billy Qigs@500wan.com
	 * @date: 2012-4-27 下午3:44:00
	 * 
	 */
	private class TimeTickerCountDown extends CountDownTimer {

		String strMin;
		String strSec;
		String strHour;
		long time;

		public TimeTickerCountDown(long millisInFuture, long countDownInterval) {
			super(millisInFuture, countDownInterval);
		}

		@Override
		public void onFinish() {

			tvTime.setText("已截止");

		}

		@Override
		public void onTick(long millisUntilFinished) {
			time = millisUntilFinished / 1000;

			// time = time % 86400;
			// 剩余小时
			strHour = String.valueOf(time / 3600);
			if (strHour.length() < 2) {
				strHour = TempData.NUM0 + strHour;
			}

			time = time % 3600;

			// 剩余分钟数
			strMin = String.valueOf(time / 60);
			if (strMin.length() < 2) {
				strMin = TempData.NUM0 + strMin;
			}

			// 剩余秒数
			strSec = String.valueOf(time % 60);
			if (strSec.length() < 2) {
				strSec = TempData.NUM0 + strSec;
			}

			String strItem = strHour + Language.POPLOTTERYTIP_HOUR + strMin + Language.POPLOTTERYTIP_MINUTE + strSec + Language.POPLOTTERYTIP_SECOND;
			// 设置颜色
			SpannableString sp = new SpannableString(strItem);
			sp.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.gcdt_txt_term)), 0, strItem.indexOf(Language.POPLOTTERYTIP_XIAO), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
			sp.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.gcdt_txt_term)), strItem.indexOf(Language.POPLOTTERYTIP_SHI) + 1,
					strItem.indexOf(Language.POPLOTTERYTIP_MINUTE), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
			sp.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.gcdt_txt_term)), strItem.indexOf(Language.POPLOTTERYTIP_MINUTE) + 1,
					strItem.indexOf(Language.POPLOTTERYTIP_SECOND), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);

			tvTime.setText(sp);

		}

	}

	// 重置倒计时时钟
	private void restartTicker(String time) {
		if (timeTicker != null) {
			timeTicker.cancel();
		}

		timeTicker = new TimeTickerCountDown(Long.parseLong(time) * 1000, 1000);
		timeTicker.start();
	}

}
